package photobox

import grails.converters.JSON
import grails.validation.ValidationException
import org.apache.commons.lang.exception.ExceptionUtils
import org.codehaus.groovy.grails.web.servlet.mvc.SynchronizerTokensHolder
import photobox.exception.ArgumentException
import photobox.exception.ConfirmException
import photobox.exception.FormTokenException
import photobox.exception.NoPermitOperationException
import photobox.exception.NotFoundException
import photobox.exception.UnauthorizeException

/**
 */
abstract class BaseController {

    def resetToken() {
        def tokensHolder = SynchronizerTokensHolder.store(session)
        def tokenUri = request.forwardURI
        def token =  tokensHolder.generateToken(tokenUri)
        def results = [code: 0, message: "", resetToken: token, resetTokenUri: tokenUri]
        render results as JSON
    }

    def handleServerException(Exception e) {
        def results
        def tokensHolder = SynchronizerTokensHolder.store(session)
        def token =  tokensHolder.generateToken(request.forwardURI)
        def tokenUri = request.forwardURI
        if (e instanceof ArgumentException) {
            results = [code: ErrorDef.ERR_CLIENT, message: e.message, resetToken: token, resetTokenUri: tokenUri]
        } else if (e instanceof NoPermitOperationException) {
            results = [code: ErrorDef.ERR_NOPERMIT, message: e.message, resetToken: token, resetTokenUri: tokenUri]
        } else if (e instanceof NotFoundException) {
            results = [code: ErrorDef.ERR_NOTFOUND, message: e.message, resetToken: token, resetTokenUri: tokenUri]
        } else if (e instanceof ValidationException) {
            results = [code: ErrorDef.ERR_VALIDATE, message: "验证错误", detail: e.errors, resetToken: token, resetTokenUri: tokenUri]
        } else if (e instanceof FormTokenException) {
            results = [code: ErrorDef.ERR_TOKEN, message: e.message]
        } else if (e instanceof ConfirmException) {
            results = [code: ErrorDef.ERR_CONFIRM, message: e.message, confirmKey: e.confirmKey, confirmMessage: e.confirmMessage, resetToken: token, resetTokenUri: tokenUri]
        } else if (e instanceof UnauthorizeException) {
            results = [code: ErrorDef.ERR_AUTHEN, message: e.message, resetToken: token, resetTokenUri: tokenUri ]
        } else if (e instanceof NumberFormatException) {
            results = [code: ErrorDef.ERR_CLIENT, message: e.message, resetToken: token, resetTokenUri: tokenUri ]
        } else {
            e.printStackTrace()
            def detail = ExceptionUtils.getFullStackTrace(e)
            try {
                //to mail admin
            } catch (Exception e1) {
                e1.printStackTrace()
            }

            results = [code: ErrorDef.ERR_SERVER, message:"参数错误或服务器内部错误!", detail: [], resetToken: token, resetTokenUri: tokenUri]
        }
        withFormat {
            json {
                render  results as JSON
            }
            '*' {
                flash.message = results.message
                redirect controller: "home", action: "showMessage"
            }
        }

    }
}
